package config;

import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;

import junit.framework.TestCase;

/**
 * Simple JUnit test class
 * 
 * @author Last G
 * 
 */
public class INIParserTests extends TestCase
{
	private static final String[] strings;
	private static final ArrayList<HashMap<String, HashMap<String, String>>> configs;
	static
	{
		strings = new String[] {
				"abc = bca",
				"abc =bca ",
				"abc=bca",
				" abc = bca ",
				// fifth
				"[General]",
				" 	 [General] 	 ",
				// Da last big test
				"Daemon = true \n" + "\n" + "# aafafs df sdf sd sd sd fsd f \n"
						+ "; sdf sdf sd fsd fsd fsd sdf sdf s \r\n"
						+ "true = false \n" + "	[General] \n"
						+ "I love this company = Balmer \n" + "[Bender] \n"
						+ "say = kiss my shiny metal ass \n"
						+ "do not = belive him \n"

		};

		ArrayList<HashMap<String, HashMap<String, String>>> arr = new ArrayList<HashMap<String, HashMap<String, String>>>();
		HashMap<String, HashMap<String, String>> n;

		// Da simple tests
		// first - forth
		n = new HashMap<String, HashMap<String, String>>();
		n.put("", new HashMap<String, String>());
		n.get("").put("abc", "bca");
		arr.add(n);
		arr.add(n);
		arr.add(n);
		arr.add(n);
		// fifth sixth
		n = new HashMap<String, HashMap<String, String>>();
		n.put("", new HashMap<String, String>());
		n.put("General", new HashMap<String, String>());
		arr.add(n);
		arr.add(n);

		// Da last big test
		n = new HashMap<String, HashMap<String, String>>();
		n.put("", new HashMap<String, String>());
		n.put("General", new HashMap<String, String>());
		n.put("Bender", new HashMap<String, String>());

		n.get("").put("Daemon", "true");
		n.get("").put("true", "false");

		n.get("General").put("I love this company", "Balmer");

		n.get("Bender").put("say", "kiss my shiny metal ass");
		n.get("Bender").put("do not", "belive him");

		arr.add(n);

		configs = arr;
	}

	/**
	 * Tests Parse(String) of the INIParser
	 * 
	 */
	public void testParseString()
	{

		for (int i = 0; i < Math.min(strings.length, configs.size()); ++i)
		{
			try
			{
				assertEquals("Assertation " + i, configs.get(i), INIParser
						.Parse(strings[i]));
			} catch (Throwable e)
			{

				fail("Assertation " + i + ": " + e.toString());
				e.printStackTrace();

			}
		}
	}

	/**
	 * Tests Parse(File) of the INIParser
	 * 
	 */
	public void testParseFile()
	{
		File name = null;
		PrintStream wrt = null;

		for (int i = 0; i < Math.min(strings.length, configs.size()); ++i)
		{
			try
			{
				name = File.createTempFile("test", null);
			} catch (Exception e)
			{
				fail("WTF?! Can't create temp file :" + e.toString());
			}
			try
			{
				wrt = new PrintStream(name);
			} catch (Exception e)
			{
				fail("WTF?! Can't open file for write:" + e.toString());
			}
			wrt.println(strings[i]);
			wrt.close();

			try
			{
				assertEquals("Assertation " + i, configs.get(i), INIParser
						.Parse(name));
			} catch (Throwable e)
			{

				fail("Assertation " + i + ": " + e.toString());
				e.printStackTrace();
			}
			name.deleteOnExit();

		}

	}

}
